// mycomponent/channelsLiveFloat/channelsLiveFloat.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    channelsLive: null, //缓存视频号信息，防止频繁调用wx.getChannelsLiveInfo

    liveSettingInfo: {}, //视频号配置信息

    is_open: false, //当前悬浮框是否开启（插件没开或者当前页面没开都为false）

    openPages: [], //页面开启集合
  },
  ready() {
    let basic_info = wx.getStorageSync("yz_basic_info");
    let plugin_setting = basic_info.plugin_setting;
    if (plugin_setting && plugin_setting["video_live_room"] && plugin_setting["video_live_room"] == 1) {
      this.getLiveSetting();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    openChannelsLive() {
      let liveInfo = this.data.channelsLive;
      wx.openChannelsLive({
        finderUserName: this.data.liveSettingInfo.video_id,
        feedId: liveInfo.feedId,
        nonceId: liveInfo.nonceId,
      });
    },

    getLiveSetting() {
      let urlStr = app.getNetAddresss("plugin.video-live-room.frontend.index.get-data");
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        data:{
          not_validate_page:0
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result != 1) return;

          this.setData({
            liveSettingInfo: res.data,
          });
          this.checkOpen();
        },
      });
    },
    checkOpen() {
      //验证当前页面是否开启显示

      if (this.data.liveSettingInfo.is_open != 1) return;

      let display_page = this.data.liveSettingInfo.display_page;
      let pageArr = display_page.split(",");

      if (pageArr.includes("1")) {
        //首页
        this.data.openPages.push("packageG/index/index");
      }
      if (pageArr.includes("2")) {
        //商品详情页
        this.data.openPages.push("packageA/detail_v2/detail_v2");
      }

      let pages = getCurrentPages();
      let page = pages[pages.length - 1];
      console.log(this.data.liveSettingInfo, "page");

      if (this.data.openPages.includes(page.route)) {
        this.getChannelsLiveInfo();
      }
    },
    getChannelsLiveInfo() {
      console.log("当前页面需要显示", this.data.liveSettingInfo);
      if (wx.getChannelsLiveInfo) {
        wx.getChannelsLiveInfo({
          finderUserName: this.data.liveSettingInfo.video_id,
          success: (res) => {
            this.data.channelsLive = res;
            this.setData({
              is_open: true,
            });
          },
          fail: (res) => {
            console.log(res);
            app.tips("请检查视频号ID与小程序是否同个主体或者视频号是否开过直播",'none',3500);
          },
        });
      }
    },
  },
});
